package com.joven.shelltest.controller;

import com.joven.shelltest.service.JovenService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * @author wzj
 * @since 2025/3/7 10:38
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/timeout")
public class TimeOutController {

    private final JovenService jovenService;

    /**
     * 可作用在controller层
     */
//    @TimeLimiter(name = "slowOperationTimeLimiter", fallbackMethod = "fallback")
//    @GetMapping("/test")
//    public CompletableFuture<String> slowEndpoint() {
//        System.out.println("----Executing slow endpoint----");
//        return CompletableFuture.supplyAsync(() -> {
//            try {
//                Thread.sleep(2000);// 模拟耗时操作
//            } catch (InterruptedException e) {
//                Thread.currentThread().interrupt();
//            }
//            return "Slow operation completed";
//        });
//    }
//
//    //回退方法与主方法返回保持一致
//    public CompletableFuture<String> fallback(Throwable t) {
//        System.out.println("Fallback method called due to timeout：" + t);
//        return CompletableFuture.completedFuture("Operation timed out, please try again later.");
//    }

    /**
     * 可作用在service层
     */
    @GetMapping("/test1")
    public CompletableFuture<String> test1() {
        return jovenService.testSlow();
    }

}
